@using ZKEACMS.Common.ViewModels
@model LayoutZonesViewModel
@{
    Layout = null;
    string returnUrl = Url.Action("Index", "Page", new { Model.PageID });
    var noZoneWidgets = Model.Widgets.Where(m => Model.Zones.All(n => n.HeadingCode != m.ZoneID));
}

@if (noZoneWidgets.Any())
{
    <div class="panel panel-danger">
        <div class="panel-heading">
            <span class="badge">@L("须要处理")</span>
        </div>
        <div class="panel-body">
            <ul id="noZoneWidgets" class="fullRowList">
                @foreach (var widget in noZoneWidgets.OrderBy(m => m.Position))
                {
                    <li id="widget_@widget.ID" class="widget" data-widgetid="@widget.ID">
                        @if (widget.LayoutID.IsNotNullAndWhiteSpace())
                        {
                            <span class="badge">@L("布局")</span>
                        }
                        @if (Authorizer.Authorize(PermissionKeys.ManagePage))
                        {
                            <a href="@Url.Action("Edit", "Widget", new { widget.ID, ReturnUrl = returnUrl})"><i class="glyphicon glyphicon-edit"></i></a>
                            <a href="javascript:void(0)" class="delete" data-widgetid="@widget.ID"><i class="glyphicon glyphicon-trash"></i></a>
                        }
                        @widget.WidgetName
                    </li>
                }
            </ul>

        </div>
        <div class="panel-footer">
            <span class="description">
                @L("组件不能正确地显示在页面中，可能是因为您切换了布局或删除了原先的区域")
            </span>
        </div>
    </div>
}

<div class="panel  panel-default">
    <div class="panel-heading">
        <span class="badge">@L("概览")</span>
        @Model.Page.Title
    </div>
    <div class="simplePreview">
        <div class="simpleDesign">
            @for (int i = 0; i < Model.LayoutHtml.Count; i++)
            {
                var itemHtml = Model.LayoutHtml[i];
                if (itemHtml.Html == ZoneEntity.ZoneTag)
                {
                    i++;
                    itemHtml = Model.LayoutHtml[i];
                    var zone = Model.Zones.Where(m => m.HeadingCode == itemHtml.Html);

                    foreach (var item in zone)
                    {
                        <div class="panel panel-success panel-relative">
                            <ul data-zoneid="@item.HeadingCode" class="fullRowList widgets connectedSortable">
                                @foreach (var widget in Model.Widgets.Where(m => m.ZoneID == item.HeadingCode).OrderBy(m => m.Position).ThenBy(m => m.WidgetName))
                                {
                                    <li id="widget_@widget.ID" class="widget" data-widgetid="@widget.ID" data-position="@(widget.LayoutID.IsNotNullAndWhiteSpace()||widget.RuleID.HasValue?widget.Position:0)">
                                        @if (widget.LayoutID.IsNotNullAndWhiteSpace())
                                        {
                                            <a class="badge" href="@Url.Action("LayoutWidget","Layout",new{ LayoutId = widget.LayoutID })">@L("布局")</a>
                                        }
                                        @if (widget.RuleID.HasValue)
                                        {
                                            <a class="badge" href="@Url.Action("Edit","Rule",new { ID = widget.RuleID })">@L("条件规则")</a>
                                        }
                                        @if (Authorizer.Authorize(PermissionKeys.ManagePage))
                                        {
                                            <a href="@Url.Action("Edit", "Widget", new { widget.ID, ReturnUrl = returnUrl })"><i class="glyphicon glyphicon-edit"></i></a>
                                            <a href="javascript:void(0)" class="delete" data-widgetid="@widget.ID"><i class="glyphicon glyphicon-trash"></i></a>
                                        }
                                        @widget.WidgetName
                                    </li>
                                }
                            </ul>
                            <div class="panel-toolbar">
                                @if (Authorizer.Authorize(PermissionKeys.ManagePage))
                                {
                                    <a class="btn btn-black btn-xs" title="@L("添加内容")" href="@Url.Action("SelectWidget", "WidgetTemplate", new { Model.PageID, ZoneID = item.HeadingCode, ReturnUrl = returnUrl})">
                                        <i class="glyphicon glyphicon-plus"></i>
                                        @L("添加内容") | @item.ZoneName
                                    </a>
                                }
                                else
                                {
                                    <a class="label label-info" title="@L("区域名称")" href="#">
                                        @item.ZoneName
                                    </a>
                                }
                            </div>
                        </div>
                    }

                    i++;
                }
                else
                {
                    @Html.Raw(itemHtml.NoStyleHtml)
                }

            }
        </div>
    </div>
    <div class="panel-footer">
        <div class="btn-group">
            @if (Authorizer.Authorize(PermissionKeys.ManagePage))
            {
                <a class="btn btn-primary" href="@Url.Action("design", new {ID = Model.PageID})">
                    <i class="glyphicon glyphicon-pencil"></i>
                    @L("编辑")
                </a>
            }
            <a class="btn btn-primary" href="@Url.Action("redirectview", "page", new { ID = Model.PageID})">
                <i class="glyphicon glyphicon-eye-open"></i>
                @L("预览编辑版本")
            </a>
            @if (Model.Page.PublishDate.HasValue)
            {
                <a class="btn btn-primary"
                   href="@Url.Action("redirectview", "page", new { ID = Model.PageID, preview = false})"
                   target="_blank">
                    <i class="glyphicon glyphicon-eye-open"></i>
                    @L("预览发布版本")
                </a>
            }
            @if (Authorizer.Authorize(PermissionKeys.ManagePage))
            {
                <a class="btn btn-primary" href="@Url.Action("edit", new {ID = Model.PageID})">
                    <i class="glyphicon glyphicon-record"></i>
                    @L("属性/历史记录")
                </a>
            }
            @if (Authorizer.Authorize(PermissionKeys.ManageLayout))
            {
                <a class="btn btn-primary" href="@Url.Action("design", "layout", new { ID = Model.LayoutID, Model.PageID})">
                    <i class="glyphicon glyphicon-th"></i>
                    @L("布局")
                </a>
            }
            @if (Authorizer.Authorize(PermissionKeys.ManagePage))
            {
                <a class="btn btn-primary publish-page" data-id="@Model.PageID" href="javascript:void(0)">
                    <i class="glyphicon glyphicon-open"></i>
                    @L("发布")
                </a>
            }

        </div>
    </div>
</div>

@if (Authorizer.Authorize(PermissionKeys.ManagePage))
{
    @await Html.PartialAsync("SortWidget.Script")
}
